<template>
  <div class="workCalendar">
    <!-- 年份和月份 -->
    <el-row type="flex" justify="end">
      <!-- 年份 -->
      <el-select
        v-model="currentYear"
        size="small"
        style="width: 120px"
        @change="dateChange"
      >
        <el-option
          v-for="item in yearList"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
      <!-- 月份 -->
      <el-select v-model="currentMonth" size="small" style="width: 120px">
        <el-option v-for="item in 12" :key="item" :label="item" :value="item" />
      </el-select>
    </el-row>
    <!-- 日历 -->
    <el-calendar v-model="value">
      <template slot="dateCell" slot-scope="{ data, date }">
        <span class="dot">{{ data.day.split('-')[2] }}</span>
        <span v-if="isWeek(date)" class="week">休</span>
      </template>
    </el-calendar>
  </div>
</template>

<script>
export default {
  name: 'WorkCalendar',
  props: {
    currentDate: {
      type: Date, // 日期类型
      default: () => new Date()
    }
  },
  data() {
    return {
      value: new Date(),
      currentYear: this.currentDate.getFullYear(),
      currentMonth: this.currentDate.getMonth() + 1
    }
  },
  computed: {
    yearList() {
      return Array.from(new Array(11), (item, index) => {
        return this.currentYear - index + 5
      })
    }
  },
  methods: {
    // 是否为周末
    isWeek(date) {
      return [6, 0].includes(date.getDay())
    },
    // 年份月份变化
    dateChange() {
      console.log(this.currentYear, this.currentMonth - 1)
      this.value = new Date(`${this.currentYear}-${this.currentMonth}-1`)
    }
  }
}
</script>

<style lang="scss" scoped>
.workCalendar {
  ::v-deep .el-calendar__header {
    display: none;
  }
  ::v-deep .el-calendar-table td {
    border: none;
  }
  ::v-deep .el-calendar-day {
    display: flex;
    justify-content: center;
    align-items: center;
    .week {
      width: 20px;
      height: 20px;
      border-radius: 10px;
      background-color: #fa7c4d;
      color: #fff;
      line-height: 20px;
      text-align: center;
      margin-left: 8px;
    }
  }
  ::v-deep .is-selected {
    .el-calendar-day {
      .dot {
        background-color: #4979fb;
        color: #fff;
        display: inline-block;
        width: 20px;
        height: 20px;
        border-radius: 10px;
        line-height: 20px;
        text-align: center;
      }
    }
  }
}
</style>
